package com.loan.psychicloan.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.loan.psychicloan.R;
import com.loan.psychicloan.Util.MyCountDownTimer;
import com.loan.psychicloan.Util.OkHttpUtils;
import com.loan.psychicloan.Util.ValidateUtil;
import com.loan.psychicloan.bean.SendMSMBean;
import com.orhanobut.logger.Logger;

/**
 * Created by watilion on 2018/1/18 0018.
 */

public class ForgetActivity extends AppCompatActivity implements View.OnClickListener{

    EditText et_forget_user;
    EditText et_forget_msg;
    EditText et_forget_psw;
    Button btn_foget_sendmsg;
    Button btn_forget_restart;
    private SendMSMBean mSendMSMBean = new SendMSMBean();
    private SendMSMBean validataNumBean =new SendMSMBean();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);

        Toolbar mToolbar = findViewById(R.id.forget_toollbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent mIntent =new Intent(getApplicationContext(),MainActivity.class);
//                startActivity(mIntent);
                finish();
            }
        });
        TextView tv_back_toolbar_title = findViewById(R.id.back_toolbar_title);
        tv_back_toolbar_title.setText("忘记密码");

        initView();
    }

    private void initView() {
        et_forget_user = findViewById(R.id.et_forget_user);
        et_forget_msg = findViewById(R.id.et_forget_msg);
        et_forget_psw = findViewById(R.id.et_forget_psw);
        btn_foget_sendmsg = findViewById(R.id.btn_foget_sendmsg);
        btn_forget_restart = findViewById(R.id.btn_forget_restart);

        btn_foget_sendmsg.setOnClickListener(this);
        btn_forget_restart.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.btn_foget_sendmsg:
                String Phonenumber = et_forget_user.getText().toString();
                if (!ValidateUtil.validatePhoneNumber(Phonenumber)){
                    Toast.makeText(this,"手机号不正确，请检查手机号",Toast.LENGTH_SHORT).show();
                }else {
                    JsonObject jsonObject = new JsonObject();
                    //jsonObject.notifyAll();
                    //jsonObject.remove("");
                    jsonObject.addProperty("phonenumber", Phonenumber);
                    jsonObject.addProperty("type","1");
                    startSendMSMTask(LoginActivity.sendmsm_url,jsonObject.toString());

                }
                break;
            case R.id.btn_forget_restart:
                if (et_forget_msg.getText() == null) {
                    Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    if (et_forget_psw == null) {
                        Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    } else if (!ValidateUtil.validatePass(et_forget_psw.getText().toString())) {
                        Toast.makeText(this, "密码不符合要求，请检查密码长度（6-12位）", Toast.LENGTH_SHORT).show();
                    } else {
                        JsonObject mRegister_JsonObject = new JsonObject();
                        mRegister_JsonObject.addProperty("phonenumber", et_forget_user.getText().toString());
                        mRegister_JsonObject.addProperty("type", "1");
                        mRegister_JsonObject.addProperty("tamp", mSendMSMBean.data.tamp);
                        mRegister_JsonObject.addProperty("hashcode", mSendMSMBean.data.hashcode);
                        mRegister_JsonObject.addProperty("code", et_forget_msg.getText().toString());
                        mRegister_JsonObject.addProperty("password", et_forget_psw.getText().toString());
                        mRegister_JsonObject.addProperty("channel", "android");
                        mRegister_JsonObject.addProperty("platform", "android");
                        Logger.json(mRegister_JsonObject.toString());
                        startForgetTask(LoginActivity.validataNumUrl, mRegister_JsonObject.toString());

                        //Toast.makeText(this,"登录",Toast.LENGTH_SHORT).show();
                    }
                }

                break;
            default:
                break;
        }
    }

    private void startForgetTask(String validataNumUrl, String json) {

        //通过类名直接调用静态方法获取单例对象再调用getBeanOfOK()方法传入参数通过接口回调获取数据
        OkHttpUtils.getInstance().postByJson(this, validataNumUrl,json, SendMSMBean.class,
                new OkHttpUtils.CallBack <SendMSMBean>() {
                    @Override
                    public void getData(SendMSMBean sendMSMBean) {
                        if (sendMSMBean != null){
                            //如果不为空就用mLoanList接收
                            validataNumBean = sendMSMBean;
                            Toast.makeText(getApplicationContext(),validataNumBean.msg,Toast.LENGTH_SHORT).show();
                            saveForget(validataNumBean);
                        }
                    }
                });
    }

    private void saveForget(SendMSMBean mValidataNumBean) {
        if (mValidataNumBean.resultcode == 10) {
            MainActivity.LOGIN = true;
            MainActivity.PHONE_NUMBER = et_forget_user.getText().toString();
            SharedPreferences sp = getSharedPreferences("config", 0);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("PHONE_NUMBER", et_forget_user.getText().toString());
            editor.putBoolean("LOGIN", true);
            editor.apply();
            Intent mIntent = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(mIntent);
            finish();

        }
    }

    public void startSendMSMTask(String mUrl, String json) {
        //通过类名直接调用静态方法获取单例对象再调用getBeanOfOK()方法传入参数通过接口回调获取数据
        OkHttpUtils.getInstance().postByJson(this, mUrl,json, SendMSMBean.class,
                new OkHttpUtils.CallBack <SendMSMBean>() {
                    @Override
                    public void getData(SendMSMBean sendMSMBean) {
                        if (sendMSMBean != null){
                            //如果不为空就用mLoanList接收
                            mSendMSMBean = sendMSMBean;
                            Toast.makeText(getApplicationContext(),mSendMSMBean.msg,Toast.LENGTH_SHORT).show();

                            setCountDownTimer(mSendMSMBean);
                        }
                    }
                });
    }

    public void setCountDownTimer(SendMSMBean mSendMSMBean) {
        if (mSendMSMBean.resultcode == 2){
            //Toast.makeText(getApplicationContext(),"tamp"+mSendMSMBean.data.tamp,Toast.LENGTH_SHORT).show();
            final MyCountDownTimer myCountDownTimer = new MyCountDownTimer(btn_foget_sendmsg,60000,1000);
            myCountDownTimer.start();
        }
    }
}
